package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderedMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.RepoetService;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RepoetServiceImpl implements RepoetService {
    @Autowired
    private OrderedMapper orderedMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    WorkspaceService workspaceService;

    @Override
    public TurnoverReportVO statistics(LocalDate begin, LocalDate end) {
        //1.查询时间段
        ArrayList<LocalDate> localDates = new ArrayList<>();
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            localDates.add(begin);
        }
        //2.查询营业额
        ArrayList<BigDecimal> bigDecimals = new ArrayList<>();
        for (LocalDate localDate : localDates) {
            BigDecimal bigDecimal = orderedMapper.sumCondition(Orders.COMPLETED, localDate);
            bigDecimals.add(bigDecimal);
        }
        TurnoverReportVO turnoverReportVO = new TurnoverReportVO();
        turnoverReportVO.setDateList(StringUtils.join(localDates, ","));
        turnoverReportVO.setTurnoverList(StringUtils.join(bigDecimals, ","));
        return turnoverReportVO;
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        //查询时间段
        ArrayList<LocalDate> localDates = new ArrayList<>();
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            localDates.add(begin);
        }
        //查询user表查询当天新增的员工数量
        ArrayList<Integer> newUserLists = new ArrayList<>();
        ArrayList<Integer> totalUserLists = new ArrayList<>();
        for (LocalDate localDate : localDates) {
            Integer newUserList = userMapper.getUserQuery(localDate);
            Integer totalUserList = userMapper.getUserSum(localDate);
            newUserLists.add(newUserList);
            totalUserLists.add(totalUserList);
        }
        //查询user表查询当天的员工总数量
        //封装UserReportVO返回
        UserReportVO userReportVO = new UserReportVO();
        userReportVO.setDateList(StringUtils.join(localDates, ","));
        userReportVO.setNewUserList(StringUtils.join(newUserLists, ","));
        userReportVO.setTotalUserList(StringUtils.join(totalUserLists, ","));
        return userReportVO;
    }

    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        //查询时间段
        ArrayList<LocalDate> localDates = new ArrayList<>();
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            localDates.add(begin);
        }
        double orderCompletionRate = 0.0;//订单完成率初始值
        Integer validOrderCount = 0;//有效订单数量
        Integer totalOrderCount = 0;//订单总数量
        //查询订单表当天的订单数量
        ArrayList<Integer> orderCountLists = new ArrayList<>();
        ArrayList<Integer> validOrderCountLists = new ArrayList<>();
        for (LocalDate localDate : localDates) {
            Map map = new HashMap();
            map.put("date", localDate);
            Integer orderCountNow = orderedMapper.getOrderNow(map);
            //查询订单表当天的有效订单数量 status = 已完成
            map.put("status", Orders.COMPLETED);
            Integer validOrderCountNow = orderedMapper.getOrderNow(map);
            orderCountLists.add(orderCountNow);
            validOrderCountLists.add(validOrderCountNow);

            //获取订单总数
            validOrderCount += validOrderCountNow;
            //获取订单有效订单数
            totalOrderCount += orderCountNow;
        }
        //获取订单表订单完成率
        orderCompletionRate += validOrderCount.doubleValue() / totalOrderCount;

        //封装OrderReportVO返回
        OrderReportVO orderReportVO = new OrderReportVO();
        orderReportVO.setOrderCompletionRate(orderCompletionRate);//订单完成率
        orderReportVO.setValidOrderCount(validOrderCount);  //有效订单数
        orderReportVO.setTotalOrderCount(totalOrderCount); //订单总数

        orderReportVO.setOrderCountList(StringUtils.join(orderCountLists, ","));//每日订单数
        orderReportVO.setDateList(StringUtils.join(localDates, ","));//日期
        orderReportVO.setValidOrderCountList(StringUtils.join(validOrderCountLists, ",")); //每日有效订单数
        return orderReportVO;
    }

    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        //查询商品名称列表
        List<GoodsSalesDTO> goodsSalesDTO = orderedMapper.selecTop10(begin, end);
        //查询商品销量列表
        ArrayList<String> nameList = new ArrayList<>();
        ArrayList<Integer> numberList = new ArrayList<>();
        for (GoodsSalesDTO salesDTO : goodsSalesDTO) {
            nameList.add(salesDTO.getName());
            numberList.add(salesDTO.getNumber());
        }
        //封装返回
        SalesTop10ReportVO salesTop10ReportVO = new SalesTop10ReportVO();
        salesTop10ReportVO.setNameList(StringUtils.join(nameList, ","));
        salesTop10ReportVO.setNumberList(StringUtils.join(numberList, ","));
        return salesTop10ReportVO;
    }

    @Override
    public void export(HttpServletResponse response) throws IOException {
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("运营数据报表模板.xlsx");
        //创建excel对象
        XSSFWorkbook excel = new XSSFWorkbook(is);
        //创建sheet页对象
        XSSFSheet sheet = excel.getSheet("Sheet1");
        //创建row行对象
        //创建cell列对象
        //获取单元格写入指定数据

        LocalDate now = LocalDate.now();
        LocalDate Date = now.minusDays(30);
        Double turnover = 0.00;
        Integer newUsers = 0;
        Integer validOrderCount = 0;
        for (int i = 0; i < 30; i++) {
            BusinessDataVO businessDataVO = workspaceService.businessData(Date);
            sheet.getRow(7 + i).getCell(1).setCellValue(Date.toString());
            sheet.getRow(7 + i).getCell(2).setCellValue(businessDataVO.getTurnover().toString());
            sheet.getRow(7 + i).getCell(3).setCellValue(businessDataVO.getValidOrderCount());
            sheet.getRow(7 + i).getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
            sheet.getRow(7 + i).getCell(5).setCellValue(businessDataVO.getUnitPrice());
            sheet.getRow(7 + i).getCell(6).setCellValue(businessDataVO.getNewUsers());
            Date = Date.plusDays(1);
            turnover += businessDataVO.getTurnover();//累加营业额
            validOrderCount += businessDataVO.getValidOrderCount();//累加月有效订单
            newUsers += businessDataVO.getNewUsers();//累加月新增用户
        }
        Map param = new HashMap();
        param.put("begin",LocalDate.now().minusDays(30));
        param.put("end",LocalDate.now().minusDays(1));
        Integer totalOrderCount = orderedMapper.getOrderNow(param);
        sheet.getRow(3).getCell(2).setCellValue(turnover);//近一个月的营业额
        sheet.getRow(3).getCell(4).setCellValue(validOrderCount.doubleValue()/totalOrderCount);//订单完成率
        sheet.getRow(3).getCell(6).setCellValue(newUsers);//进一个月新增的用户数据
        sheet.getRow(4).getCell(2).setCellValue(validOrderCount);//近一个月的有效订单
        sheet.getRow(4).getCell(4).setCellValue(turnover.doubleValue()/validOrderCount);//近一个月的平均单价



        //将excel表格对象 输出到指定盘符

        excel.write(response.getOutputStream());
        excel.close();
        System.out.println("写入成功");


    }
}
